package com.techwells.applicationMarket.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.techwells.applicationMarket.domain.Message;
import com.techwells.applicationMarket.properties.ConfigProperties;
import com.techwells.applicationMarket.service.MessageService;
import com.techwells.applicationMarket.util.PagingTool;
import com.techwells.applicationMarket.util.ResultInfo;

/**
 * 消息的controller
 * @author Administrator
 *
 */
@RestController
@RequestMapping("*.do")    //配置访问的后缀，只有后缀为.do的url才能访问到接口
public class MessageController {
	@Resource
	private MessageService messageService;
	
	@Resource
	private ConfigProperties configProperties;
	
	
	private static final Logger log=LoggerFactory.getLogger(MessageController.class);
	
	/**
	 * 添加消息
	 * @param title 标题
	 * @param content  内容
	 * @param request
	 * @return
	 */
	@RequestMapping("/message/addMessage")
	public Object addMessage(HttpServletRequest request){
		ResultInfo resultInfo=new ResultInfo();
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		
		//标题
		if (StringUtils.isEmpty(title)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("标题不能为空");
			return resultInfo;
		}
		
		
		if (StringUtils.isEmpty(content)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("内容给不能为空");
			return resultInfo;
		}
		
		Message message=new Message();
		message.setContent(content);
		message.setCreateDate(new Date());
		message.setTitle(title);
		//系统通知
		message.setType(1);  
		//设置logo
		message.setUrl(configProperties.getMessageLogo());  
		
		try {
			Object object=messageService.addMessage(message);
			return object;
		} catch (Exception e) {
			log.error("添加异常",e);
			resultInfo.setCode("-1");
			resultInfo.setMessage("添加异常");
			return resultInfo;
		}
	}
	
	
	/**
	 * 获取消息详情
	 * @param request
	 * @return
	 */
	@RequestMapping("/message/getMessage")
	public Object getMessage(HttpServletRequest request){
		ResultInfo resultInfo=new ResultInfo();
		String messageId=request.getParameter("messageId");  //消息Id
		String userId=request.getParameter("userId");  //用户id
		
		if (StringUtils.isEmpty(messageId)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("消息Id不能为空");
			return resultInfo;
		}
		
//		if (StringUtils.isEmpty(userId)) {
//			resultInfo.setCode("-1");
//			resultInfo.setMessage("用户Id不能为空");
//			return resultInfo;
//		}
		
		//调用servce层的方法
		try {
			if (!StringUtils.isEmpty(userId)) {
				Object object=messageService.getMessage(Integer.parseInt(messageId),Integer.parseInt(userId));
				return object;
			}else {
				Object object=messageService.getMessage(Integer.parseInt(messageId),null);
				return object;
			}
			
		} catch (Exception e) {
			log.error("获取消息异常",e);
			resultInfo.setCode("-1");
			resultInfo.setMessage("异常");
			return resultInfo;
		}
	}
	
	
	/**
	 * 分页获取消息列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/message/getMessageList")
	public Object getMessageList(HttpServletRequest request){
		ResultInfo resultInfo=new ResultInfo();
		String pageNum=request.getParameter("pageNum");
		String pageSize=request.getParameter("pageSize");
		
		if (StringUtils.isEmpty(pageNum)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("当前页数不能为空");
			return resultInfo;
		}
		
		if (StringUtils.isEmpty(pageSize)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("每页显示的数量不能为空");
			return resultInfo;
		}
		
		
		PagingTool pagingTool=new PagingTool(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
		Map<String, Object> params=new HashMap<String, Object>();
		
		pagingTool.setParams(params);
		
		try {
			Object object=messageService.getMessageList(pagingTool);
			return object;
		} catch (Exception e) {
			log.error("获取消息列表异常",e);
			resultInfo.setCode("-1");
			resultInfo.setMessage("异常");
			return resultInfo;
		}
	}
	
	
	/**
	 * 	用户分页获取消息列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/message/getMessageListByUserId")
	public Object getMessageListByUserId(HttpServletRequest request){
		ResultInfo resultInfo=new ResultInfo();
		String pageNum=request.getParameter("pageNum");
		String pageSize=request.getParameter("pageSize");
		String userId=request.getParameter("userId"); //用户Id
		String type=request.getParameter("type");  //消息类型 1 系统消息，2 钱包消息
		
		if (StringUtils.isEmpty(pageNum)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("当前页数不能为空");
			return resultInfo;
		}
		
		if (StringUtils.isEmpty(pageSize)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("每页显示的数量不能为空");
			return resultInfo;
		}
		
		if (StringUtils.isEmpty(userId)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("用户Id不能为空");
			return resultInfo;
		}
		
		if (StringUtils.isEmpty(type)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("消息类型不能为空");
			return resultInfo;
		}
		
		
		PagingTool pagingTool=new PagingTool(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("userId", Integer.parseInt(userId));
		params.put("type", Integer.parseInt(type));
		pagingTool.setParams(params);
		
		try {
			Object object=messageService.getMessageListByUserId(pagingTool);
			return object;
		} catch (Exception e) {
			log.error("获取消息列表异常",e);
			resultInfo.setCode("-1");
			resultInfo.setMessage("异常");
			return resultInfo;
		}
	}
 	
	/**
	 * 删除消息
	 * @param messageId  消息Id
	 * @param request
	 * @return
	 */
	@RequestMapping("/message/delete")
	public Object deleteMessage(HttpServletRequest request){
		ResultInfo resultInfo=new ResultInfo();
		String messageId=request.getParameter("messageId"); 
		if (StringUtils.isEmpty(messageId)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("消息Id不能为空");
			return resultInfo;
		}
		
		try {
			Object object=messageService.delete(Integer.parseInt(messageId));
			return object;
		} catch (Exception e) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("异常");
			return resultInfo;
		}
		
	}
	
	/**
	 * 修改消息
	 * @param request
	 * @return
	 */
	@RequestMapping("/message/modify")
	public Object modify(HttpServletRequest request){
		ResultInfo resultInfo=new ResultInfo();
		String messageId=request.getParameter("messageId");  //消息id
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		
		//标题
		if (StringUtils.isEmpty(title)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("标题不能为空");
			return resultInfo;
		}
		
		if (StringUtils.isEmpty(content)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("内容不能为空");
			return resultInfo;
		}
		
		Message message=new Message();
		message.setMessageId(Integer.parseInt(messageId));
		message.setContent(content);
		message.setTitle(title);
		message.setUrl(configProperties.getMessageLogo());
		message.setType(1);
		
		try {
			Object object=messageService.modify(message);
			return object;
		} catch (Exception e) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("添加异常");
			return resultInfo;
		}
	}
	
	
	/**
	 *	 用户删除消息
	 */
	@RequestMapping("/message/deleteMessageByUserId")
	public Object deleteMessageByUserId(HttpServletRequest request){
		ResultInfo resultInfo=new ResultInfo();
		String messageId=request.getParameter("messageId");  //消息Id
		String userId=request.getParameter("userId"); //用户Id
		
		if (StringUtils.isEmpty(messageId)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("消息Id不能为空");
			return resultInfo;
		}
		
		if (StringUtils.isEmpty(userId)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("用户Id不能为空");
			return resultInfo;
		}
		
		try {
			Object object=messageService.deleteMessageByUserId(Integer.parseInt(userId),Integer.parseInt(messageId));
			return object;
		} catch (Exception e) {
			log.error("删除消息异常",e);
			resultInfo.setCode("-1");
			resultInfo.setMessage("异常");
			return resultInfo;
		}
		
	}
	
	/**
	 * 获取未读消息总数
	 * @param request
	 * @return
	 */
	@RequestMapping("/message/getMessageTotal")
	public Object getMessageTotal(HttpServletRequest request) {
		ResultInfo resultInfo = new ResultInfo();
		String userId=request.getParameter("userId");  //用户Id
		if (StringUtils.isEmpty(userId)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("用户Id不能为空");
			return resultInfo;
		}
		
		return messageService.getMessageTotal(Integer.parseInt(userId));
	}
	
	
	
}
